package com.zlsy.thread.joinlearing;

import lombok.extern.slf4j.Slf4j;

import java.util.concurrent.TimeUnit;

/**
 * @author zhouliang
 * @date 2020/3/7 18:51
 */
@Slf4j
public class JoinStateThread {

    // 主线程在等待子线程的时候属于线程的什么状态 ？

    public static void main(String[] args) {
        Thread mainThread = Thread.currentThread();
        Thread thread = new Thread(new Runnable() {
            @Override
            public void run() {
                try {
                    log.info("主线程在子线程进行sleep之前为:{}", mainThread.getState());
                    TimeUnit.SECONDS.sleep(3);
                    //为什么要在睡眠之后再查看主线程的状态？
                    //因为保证子线程肯定是在等待状态
                    log.info("主线程在子线程进行sleep之后为:{}", mainThread.getState());
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        });
        //子线程启动
        thread.start();
        try {
            thread.join();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        //子线程执行完了，这时候主线程状态？
        log.info("{}",mainThread.getState());
    }
}
